//
//  ViewController.m
//  ui豆瓣
//
//  Created by lirui on 15/12/23.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import "activemodel.h"
#import "activityTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "active2ViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic ,retain)UITableView *tableview;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)NSDictionary *dic;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view .backgroundColor = [ UIColor whiteColor];
    _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableview.backgroundColor = [ UIColor whiteColor];
    [self.view addSubview:self.tableview];
    //设置边界距离
    self.tableview.contentInset = UIEdgeInsetsMake(0, 0,64, 0);
    self.tableview .dataSource= self;
    self.tableview.delegate = self;
    self.navigationItem.title = @"活动";
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    //导航栏背景图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg.png"]forBarMetrics:UIBarMetricsDefault];
    //遍历数据源
    // 数据处理的目的 : 数组+字典 -> 数组+对象

    [self datahandeler];
    
    //注册
    [self.tableview registerClass:[ activityTableViewCell class] forCellReuseIdentifier:@"cell"];
    //self.navigationItem.rightBarButtonItem = self.editButtonItem;

}
-(void)datahandeler
{
    NSString *str = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
    NSString *urlstr =[ str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:str]];
    NSURL *url =[ NSURL URLWithString:urlstr];
    NSURLRequest *request =[ NSURLRequest requestWithURL:url];
    NSURLSession *session = [ NSURLSession sharedSession];
    NSURLSessionDataTask *task =[ session  dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            id result = [ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *arr = result[@"events"];
            self.dataArr = [NSMutableArray array];
            self.dataArr =[activemodel modelHanderWithArray:arr];
            NSLog(@"%@",_dataArr);
            [self.tableview reloadData];
        });
    }];
    
    [task resume];
}

//tableview 的协议方法
//自定义cell的行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 210;
}
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return _dataArr.count;
}
//创建cell 赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   activityTableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
 activemodel *m = _dataArr[ indexPath.row];
    
    cell.lovelabel.textColor = [ UIColor redColor];
    cell.join.textColor = [UIColor redColor];
    
    cell.activename.text = m.title;
    cell.adress.text = m.address;
    cell.time.text = m.begin_time;
    cell.style.text = m.category_name;
    cell.lovelabel.text =[NSString stringWithFormat:@"感兴趣: %@", m.wisher_count];
    cell.join.text = [NSString stringWithFormat:@"参加: %@",m.participant_count];
   [ cell.activeimg sd_setImageWithURL:[NSURL URLWithString:m.image]];
    
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{active2ViewController *actVC2 = [[ active2ViewController alloc] init];
   
    actVC2.md = _dataArr[indexPath.row] ;
    //隐藏tabbar 底部栏
    actVC2.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:actVC2 animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
